<?php

namespace App\HttpController\XAppWxapp\WxappXK\Web;


use App\HttpController\ControllerWxApp;
use App\Model\t_a_xf_room;
use App\Model\t_wx_user_a_xf;
use XWX\Common\Validate\Validate;

class ApiUser extends ControllerWxApp
{
    /** 我的记录 */
    function index()
    {
        $entity_wxuser = $this->funcGetWxUser();
        $entity_wxuser_xf = $this->funcGetWxUserxf();


        $user = [
            'wxuser_nickname' => $entity_wxuser->wxuser_nickname,
            'wxuser_headimgurl' => $entity_wxuser->wxuser_headimgurl,
        ];

        $this->commonR()->setData('user', $user);
        $this->commonR()->setData('wxuserxf_phone', $entity_wxuser_xf->wxuserxf_phone);
        $this->commonR()->setData('wxuserxf_room_count', $entity_wxuser_xf->wxuserxf_room_count);
        $this->commonR()->setData('wxuserxf_comment_count', $entity_wxuser_xf->wxuserxf_comment_count);

        $this->funcWriteCommonR();
    }


    /** 注册或者修改用户信息 */
    function edit()
    {
        $phone = $this->reqParam('phone');


        $v = new Validate();
        $v->addColumn('school')->required('学校必填')->lenMax(50);
        $v->addColumn('name')->required('姓名必填')->lenMax(50);
        $v->addColumn('subject')->required('科目必填')->lenMax(50);
        $v->addColumn('teacher_age')->required('教龄必填');

        $v->addColumn('phone')->required('手机必填')->lenMax(20)
            ->func(function ($val)
            {
                $entity = t_wx_user_a_xf::funcGetUserByPhone($val);
                if ($entity == null || $entity->wxuserxf_id == $this->getUserID())
                {
                    return $this->funcGetR(0);
                }

                return $this->funcGetR(-1043, '手机号已注册过');
            });
        if (!$v->validate($this->reqParam()))
        {
            $r = current($v->getErrors());
            return $this->funcWriteErrR($r);
        }


        $data = [];
        foreach ($this->reqParam() as $k => $v)
        {
            $data[t_wx_user_a_xf::prefix() . $k] = $v;
        }


        $entity = t_wx_user_a_xf::funcGetUserByPhone($phone);
        if ($entity === null)
        {
            $entity = new t_wx_user_a_xf();
            $entity->fill($data);
            $entity->funcSetX($this->funcGetX());
            $entity->wxuserxf_id = $this->getUserID();
            $entity->funcInsert();
        }
        else
        {
            $entity->fill($data, true);
            $entity->funcUpdate();
        }


        $this->commonR()->setData('entity_userxf', $entity);

        $this->funcWriteCommonR();
    }


    /** 我的开课房间 */
    function listRoom()
    {
        $query = $this->db()
            ->where('xfroom_x_id', $this->funcGetX())
            ->where('xfroom_wxuser_id', $this->getUserID())
            ->orderBy('xfroom_start_date');

        $list = $query->get(t_a_xf_room::table(), [$this->funcGetPageIndexXPage(), $this->funcGetPageSize()]);


        $this->commonR()->setData('list', $list);


        $this->funcWriteCommonR();
    }


    /** 我的评课记录 */
    function listComment()
    {

    }
}